package me.khrystal.rxnews.base;

import android.graphics.Rect;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;

/**
 * usage:
 * author: kHRYSTAL
 * create time: 16/4/8
 * update time:
 * email: 723526676@qq.com
 */
public class BaseSpacesItemDecoration extends RecyclerView.ItemDecoration{

    private int mSpace;

    public BaseSpacesItemDecoration(int space){
        this.mSpace = space;
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        int position = parent.getChildAdapterPosition(view);
        outRect.left = mSpace;
        outRect.right = mSpace;
        outRect.top = 0;
        outRect.bottom = mSpace;

        if (parent.getLayoutManager() instanceof StaggeredGridLayoutManager){
            outRect.left = 0;
            outRect.right = 0;
            outRect.top = 0;
            outRect.bottom = 0;
            if (position < ((StaggeredGridLayoutManager) parent.getLayoutManager())
                    .getSpanCount()){
//                比如共有3列 那么0，1，2位置的position会执行到这个判断内部
//                即设置顶部距离
                outRect.top = mSpace;
            }else if (parent.getLayoutManager() instanceof GridLayoutManager){
                if (position < ((GridLayoutManager)parent.getLayoutManager())
                        .getSpanCount()){
                    outRect.top = mSpace;
                }
            }else if (parent.getLayoutManager() instanceof LinearLayoutManager){
                if (position == 0){
                    outRect.top = mSpace;
                }
            }
        }
    }
}
